package com.jiyinit.web.core.video.manager;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.base.util.Check;
import com.jiyinit.core.framework.base.util.JsonEntityTransform;
import com.jiyinit.web.core.video.dto.IconMainRequest;
import com.jiyinit.web.core.video.entity.TabCoreIconMainEntity;
import com.jiyinit.web.core.video.service.IconMainServiceImpl;

@Controller
@RequestMapping(value = "/mobileIconMain")
public class MobileIconMainController {

    private static Logger logger = LoggerFactory.getLogger(MobileIconMainController.class);

    @Resource(name = "core.iconMainServiceImpl")
    private IconMainServiceImpl service;

    /**
     * 查询页面
     * @return
     */
    @RequestMapping(value = "/viconList", method = RequestMethod.GET)
    public String list(){
        return "/mobile/viconList";
    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String toAdd(){
        return "/mobile/viconAdd";
    }

    /**,
     * 分页查询
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/viconList", method=RequestMethod.POST)
    public PagingResult<TabCoreIconMainEntity> getPageRows(HttpServletRequest req, IconMainRequest request){
        logger.info("转换为obj之后的参数: {}", request.toString());
        request.setLimit(10);
        PagingResult<TabCoreIconMainEntity> pag = service.findForPage(request);
        return pag;
    }

    /**
     * 添加
     * @param req
     * @returnw
     */
    @ResponseBody
    @RequestMapping(value = "/toAdd", method=RequestMethod.POST)
    public ResponseEntity<ResponseDto> add(HttpServletRequest req, TabCoreIconMainEntity entity){
        logger.info("转换为obj之后的参数: {}", entity.toString());
        int i = 0;
        if(!Check.isNullObj(entity)){
            i = service.add(entity);
            logger.info("添加之后的json串:{}", JsonEntityTransform.object2Json(entity));
        }
        if (i>0) {
            return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
        }
        return new ResponseEntity<>(ResponseDto.responseFail("添加失败"),HttpStatus.OK);
    }

    /**
     * 修改
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method=RequestMethod.POST)
    public ResponseEntity<ResponseDto> update(HttpServletRequest req, TabCoreIconMainEntity entity){
        logger.info("转换为obj之后的参数: {}", entity.toString());
        int i = 0;
        if(!Check.isNullObj(entity)){
            i = service.update(entity);
            logger.info("更新之后的json串:{}", JsonEntityTransform.object2Json(entity));
        }
        if (i>0) {
            return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
        }
        return new ResponseEntity<>(ResponseDto.responseFail(""),HttpStatus.OK);
    }

    /**
     * 删除
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method=RequestMethod.POST)
    public ResponseEntity<ResponseDto> delete(HttpServletRequest req, Integer ids) {
        logger.info("传入参数: {}", ids);
        int num= service.delete(ids);
        if (num > 0) {
            return new ResponseEntity<>(ResponseDto.responseOK("操作成功"), HttpStatus.OK);
        }
        return new ResponseEntity<>(ResponseDto.responseFail("操作失败"), HttpStatus.OK);
    }

    /**
     * 跳到修改页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/edit")
    public String edit(Model model, HttpServletRequest request, Integer id) {
        TabCoreIconMainEntity entity= service.findOne(id);
        request.setAttribute("mobileIconMain", entity);
        return "/mobile/viconEdit";
    }
}
